package com.nowcoder.project.controllers;

import com.github.pagehelper.Page;
import com.nowcoder.project.model.Book;
import com.nowcoder.project.model.User;
import com.nowcoder.project.service.BookService;
import com.nowcoder.project.service.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by nowcoder on 2018/08/04 下午3:41
 */
@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private HostHolder hostHolder;

    @RequestMapping(path = {"/index"}, method = {RequestMethod.GET})
    public String bookList(Model model) {

        User host = hostHolder.getUser();
        if (host != null) {
            model.addAttribute("host", host);
        }
        loadAllBooksView(model);
        return "book/books";

    }

    @RequestMapping(path = {"/api/books"}, method = {RequestMethod.GET})
    @ResponseBody
    public List<Book> bookList2() {

        List<Book> bookList = bookService.getAllBooks();
        return bookList;

    }

    @ResponseBody
    @GetMapping("/api/bookpage")
    public Map<String, Object> queryBookByPage(Integer pageNum, Integer pageSize) {
        System.out.println("pageNum:" + pageNum);
        System.out.println("pageSize" + pageSize);
        Page<Book> page = bookService.getBooksByPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("books", page);
        map.put("totalCount", page.getTotal());
        return map;
    }


    @RequestMapping(path = {"/api/books"}, method = {RequestMethod.POST})
    @ResponseBody
    public void addBook2(@RequestBody Map map) {

        System.out.println("name=" + (String) map.get("name") + "author=" + (String) map.get("author") + "price" + (String) map.get("price"));
        Book book = new Book();
        book.setName((String) map.get("name"));
        book.setAuthor((String) map.get("author"));
        book.setPrice((String) map.get("price"));
        bookService.addBooks(book);
    }

    @RequestMapping(path = {"api/books/{bookId:[0-9]+}"}, method = {RequestMethod.DELETE})
    @ResponseBody
    public void deleteBook2(
            @PathVariable("bookId") int bookId
    ) {

        System.out.println(bookId);
        bookService.deleteBooks(bookId);
        //return ResponseEntity.status(HttpStatus.OK).build();

    }

    @RequestMapping(path = {"api/books/{bookId:[0-9]+}"}, method = {RequestMethod.PUT})
    @ResponseBody
    public void updateBook2(
            @PathVariable("bookId") int bookId, @RequestBody Map map
    ) {

        System.out.println(bookId);
        Book book = new Book();
        book.setId(bookId);
        book.setName((String) map.get("name"));
        book.setAuthor((String) map.get("author"));
        book.setPrice((String) map.get("price"));
        bookService.updateBook(book);
    }

    @RequestMapping(path = {"/books/add"}, method = {RequestMethod.GET})
    public String addBook() {
        return "book/addbook";
    }


    @RequestMapping(path = {"/books/add/do"}, method = {RequestMethod.POST})
    public String doAddBook(
            @RequestParam("name") String name,
            @RequestParam("author") String author,
            @RequestParam("price") String price
    ) {

        Book book = new Book();
        book.setName(name);
        book.setAuthor(author);
        book.setPrice(price);
        bookService.addBooks(book);

        return "redirect:/index";

    }

    @RequestMapping(path = {"/books/{bookId:[0-9]+}/delete"}, method = {RequestMethod.GET})
    public String deleteBook(
            @PathVariable("bookId") int bookId
    ) {

        bookService.deleteBooks(bookId);
        return "redirect:/index";

    }

    @RequestMapping(path = {"/books/{bookId:[0-9]+}/recover"}, method = {RequestMethod.GET})
    public String recoverBook(
            @PathVariable("bookId") int bookId
    ) {

        bookService.recoverBooks(bookId);
        return "redirect:/index";

    }

    /**
     * 为model加载所有的book
     */
    private void loadAllBooksView(Model model) {
        model.addAttribute("books", bookService.getAllBooks());
    }

}
